<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>介绍 | Vue.js</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link href="https://fonts.googleapis.com/css?family=Inter:300,400,500,600|Open+Sans:400,600;display=swap" rel="stylesheet">
    <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="icon" href="/logo.png">
    <script src="https://player.youku.com/iframeapi"></script>
    <meta name="description" content="Vue.js - The Progressive JavaScript Framework">
    <link rel="preload" href="/assets/css/0.styles.60414370.css" as="style"><link rel="preload" href="/assets/js/app.0a9bf1a0.js" as="script"><link rel="preload" href="/assets/js/9.f307b0a5.js" as="script"><link rel="preload" href="/assets/js/18.9c9769d1.js" as="script"><link rel="preload" href="/assets/js/10.523605bb.js" as="script"><link rel="preload" href="/assets/js/85.02c708f2.js" as="script"><link rel="prefetch" href="/assets/js/1.832c44bf.js"><link rel="prefetch" href="/assets/js/100.6c7a3206.js"><link rel="prefetch" href="/assets/js/101.656addb5.js"><link rel="prefetch" href="/assets/js/102.fddd4c59.js"><link rel="prefetch" href="/assets/js/103.8b570fcc.js"><link rel="prefetch" href="/assets/js/104.b1a5605a.js"><link rel="prefetch" href="/assets/js/105.edde2e45.js"><link rel="prefetch" href="/assets/js/106.3b04c318.js"><link rel="prefetch" href="/assets/js/107.98cab24c.js"><link rel="prefetch" href="/assets/js/108.15d2ab25.js"><link rel="prefetch" href="/assets/js/109.5f483cd0.js"><link rel="prefetch" href="/assets/js/11.43202c8c.js"><link rel="prefetch" href="/assets/js/110.de44b32c.js"><link rel="prefetch" href="/assets/js/111.e247818d.js"><link rel="prefetch" href="/assets/js/112.f330b33d.js"><link rel="prefetch" href="/assets/js/113.d26db724.js"><link rel="prefetch" href="/assets/js/114.4a085227.js"><link rel="prefetch" href="/assets/js/115.6f41e46c.js"><link rel="prefetch" href="/assets/js/116.d9f31fc2.js"><link rel="prefetch" href="/assets/js/117.6bc037ec.js"><link rel="prefetch" href="/assets/js/118.b6d13098.js"><link rel="prefetch" href="/assets/js/119.40081175.js"><link rel="prefetch" href="/assets/js/12.9ac43f9f.js"><link rel="prefetch" href="/assets/js/120.dc59e2a2.js"><link rel="prefetch" href="/assets/js/121.5d6c245d.js"><link rel="prefetch" href="/assets/js/122.44eb5e5a.js"><link rel="prefetch" href="/assets/js/123.2fbebc5c.js"><link rel="prefetch" href="/assets/js/124.9733dae9.js"><link rel="prefetch" href="/assets/js/125.35fe073b.js"><link rel="prefetch" href="/assets/js/126.b2d1ea91.js"><link rel="prefetch" href="/assets/js/127.3d7f3105.js"><link rel="prefetch" href="/assets/js/128.aa600ea3.js"><link rel="prefetch" href="/assets/js/129.92b123b0.js"><link rel="prefetch" href="/assets/js/13.61cfe0ac.js"><link rel="prefetch" href="/assets/js/130.65930d6d.js"><link rel="prefetch" href="/assets/js/131.fe452d41.js"><link rel="prefetch" href="/assets/js/132.8db41894.js"><link rel="prefetch" href="/assets/js/133.2422a318.js"><link rel="prefetch" href="/assets/js/134.28ae1eb3.js"><link rel="prefetch" href="/assets/js/135.854307ee.js"><link rel="prefetch" href="/assets/js/136.42e4e007.js"><link rel="prefetch" href="/assets/js/137.c557b795.js"><link rel="prefetch" href="/assets/js/138.8314e9f0.js"><link rel="prefetch" href="/assets/js/139.4a91ed8c.js"><link rel="prefetch" href="/assets/js/14.c6ae250c.js"><link rel="prefetch" href="/assets/js/140.5be2f667.js"><link rel="prefetch" href="/assets/js/141.02fbd174.js"><link rel="prefetch" href="/assets/js/142.f1c4e87e.js"><link rel="prefetch" href="/assets/js/143.59791aa7.js"><link rel="prefetch" href="/assets/js/144.fb6601d0.js"><link rel="prefetch" href="/assets/js/145.11777ee6.js"><link rel="prefetch" href="/assets/js/15.99a67fb3.js"><link rel="prefetch" href="/assets/js/16.54274551.js"><link rel="prefetch" href="/assets/js/17.620b927c.js"><link rel="prefetch" href="/assets/js/19.3a6eb738.js"><link rel="prefetch" href="/assets/js/2.10e2fb64.js"><link rel="prefetch" href="/assets/js/20.4f240fd5.js"><link rel="prefetch" href="/assets/js/21.b5c24ec0.js"><link rel="prefetch" href="/assets/js/22.6b36d53a.js"><link rel="prefetch" href="/assets/js/23.0742dc98.js"><link rel="prefetch" href="/assets/js/24.490994ab.js"><link rel="prefetch" href="/assets/js/25.854225c9.js"><link rel="prefetch" href="/assets/js/26.cb8bede1.js"><link rel="prefetch" href="/assets/js/27.01fe0de5.js"><link rel="prefetch" href="/assets/js/28.8d33f619.js"><link rel="prefetch" href="/assets/js/29.1005b103.js"><link rel="prefetch" href="/assets/js/3.4ba5caef.js"><link rel="prefetch" href="/assets/js/30.0629b722.js"><link rel="prefetch" href="/assets/js/31.41c2c5b0.js"><link rel="prefetch" href="/assets/js/32.7709b53d.js"><link rel="prefetch" href="/assets/js/33.7e973829.js"><link rel="prefetch" href="/assets/js/34.107475bf.js"><link rel="prefetch" href="/assets/js/35.e83bdd9b.js"><link rel="prefetch" href="/assets/js/36.e0922c67.js"><link rel="prefetch" href="/assets/js/37.af806c96.js"><link rel="prefetch" href="/assets/js/38.94bae313.js"><link rel="prefetch" href="/assets/js/39.f15cd956.js"><link rel="prefetch" href="/assets/js/4.b65b2ec4.js"><link rel="prefetch" href="/assets/js/40.18818521.js"><link rel="prefetch" href="/assets/js/41.cc39555d.js"><link rel="prefetch" href="/assets/js/42.eb469d2e.js"><link rel="prefetch" href="/assets/js/43.7bdc7f53.js"><link rel="prefetch" href="/assets/js/44.c15154a9.js"><link rel="prefetch" href="/assets/js/45.e28b1e26.js"><link rel="prefetch" href="/assets/js/46.1a582225.js"><link rel="prefetch" href="/assets/js/47.1681e4e7.js"><link rel="prefetch" href="/assets/js/48.c7e4df2a.js"><link rel="prefetch" href="/assets/js/49.24a40e32.js"><link rel="prefetch" href="/assets/js/5.436c63ff.js"><link rel="prefetch" href="/assets/js/50.79ca1e35.js"><link rel="prefetch" href="/assets/js/51.4d6124d9.js"><link rel="prefetch" href="/assets/js/52.93b75fa3.js"><link rel="prefetch" href="/assets/js/53.99ab0219.js"><link rel="prefetch" href="/assets/js/54.91605e00.js"><link rel="prefetch" href="/assets/js/55.d1fdbf6f.js"><link rel="prefetch" href="/assets/js/56.e162390f.js"><link rel="prefetch" href="/assets/js/57.af6ab199.js"><link rel="prefetch" href="/assets/js/58.71eff2a5.js"><link rel="prefetch" href="/assets/js/59.b70c19c9.js"><link rel="prefetch" href="/assets/js/6.1bc8dee2.js"><link rel="prefetch" href="/assets/js/60.03187705.js"><link rel="prefetch" href="/assets/js/61.c9596d10.js"><link rel="prefetch" href="/assets/js/62.28b9a2e8.js"><link rel="prefetch" href="/assets/js/63.a5f17acf.js"><link rel="prefetch" href="/assets/js/64.deca5d49.js"><link rel="prefetch" href="/assets/js/65.8014765f.js"><link rel="prefetch" href="/assets/js/66.c136f2b4.js"><link rel="prefetch" href="/assets/js/67.55b8c4d6.js"><link rel="prefetch" href="/assets/js/68.ff568b2f.js"><link rel="prefetch" href="/assets/js/69.cbb0de95.js"><link rel="prefetch" href="/assets/js/70.9a450c62.js"><link rel="prefetch" href="/assets/js/71.56ddc8d7.js"><link rel="prefetch" href="/assets/js/72.ff8c82c4.js"><link rel="prefetch" href="/assets/js/73.5a3ae71c.js"><link rel="prefetch" href="/assets/js/74.d43f66e7.js"><link rel="prefetch" href="/assets/js/75.a0490d28.js"><link rel="prefetch" href="/assets/js/76.51277fc3.js"><link rel="prefetch" href="/assets/js/77.12725190.js"><link rel="prefetch" href="/assets/js/78.e3426443.js"><link rel="prefetch" href="/assets/js/79.34f80eb5.js"><link rel="prefetch" href="/assets/js/80.e7d07c97.js"><link rel="prefetch" href="/assets/js/81.497c1c98.js"><link rel="prefetch" href="/assets/js/82.5edfa1e6.js"><link rel="prefetch" href="/assets/js/83.72c9f367.js"><link rel="prefetch" href="/assets/js/84.72494822.js"><link rel="prefetch" href="/assets/js/86.75fdd91f.js"><link rel="prefetch" href="/assets/js/87.431ab614.js"><link rel="prefetch" href="/assets/js/88.06c97259.js"><link rel="prefetch" href="/assets/js/89.1c6506f7.js"><link rel="prefetch" href="/assets/js/90.7f074177.js"><link rel="prefetch" href="/assets/js/91.82276ba6.js"><link rel="prefetch" href="/assets/js/92.2d5fa7b8.js"><link rel="prefetch" href="/assets/js/93.9ae1367c.js"><link rel="prefetch" href="/assets/js/94.3ba6340b.js"><link rel="prefetch" href="/assets/js/95.e31bf445.js"><link rel="prefetch" href="/assets/js/96.b6f4c3e4.js"><link rel="prefetch" href="/assets/js/97.fd612974.js"><link rel="prefetch" href="/assets/js/98.fc2e9816.js"><link rel="prefetch" href="/assets/js/99.ea1b3d86.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.a100821a.js">
    <link rel="stylesheet" href="/assets/css/0.styles.60414370.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><!----> <header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Vue.js" class="logo"> <span class="site-name can-hide">Vue.js</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="/guide/migration/introduction.html" class="nav-link">
  v3 迁移指南
</a></li><li class="dropdown-item"><!----> <a href="/style-guide/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="/cookbook/" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="/examples/markdown.html" class="nav-link">
  示例
</a></li></ul></div></div><div class="nav-item"><a href="/api/application-config.html" class="nav-link">
  API Reference
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/community/team/" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="/community/partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="/community/join/" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="/community/themes/" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://next.router.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuex.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://cli.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuejs.github.io/vue-test-utils-next-docs/guide/introduction.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://github.com/vuejs/vue-devtools" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://news.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/support-vuejs/#one-time-donations" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="/support-vuejs/#recurring-pledges" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://osholic.com/?utm_source=vue&amp;utm_medium=dropdown" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://vue.threadless.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://v3.vuejs.org" target="_blank" rel="noopener noreferrer" class="nav-link external">
  English
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/vuejs/docs-next-zh-cn" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <!----></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="/guide/migration/introduction.html" class="nav-link">
  v3 迁移指南
</a></li><li class="dropdown-item"><!----> <a href="/style-guide/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="/cookbook/" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="/examples/markdown.html" class="nav-link">
  示例
</a></li></ul></div></div><div class="nav-item"><a href="/api/application-config.html" class="nav-link">
  API Reference
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/community/team/" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="/community/partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="/community/join/" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="/community/themes/" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://next.router.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuex.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://cli.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuejs.github.io/vue-test-utils-next-docs/guide/introduction.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://github.com/vuejs/vue-devtools" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://news.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/support-vuejs/#one-time-donations" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="/support-vuejs/#recurring-pledges" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://osholic.com/?utm_source=vue&amp;utm_medium=dropdown" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://vue.threadless.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://v3.vuejs.org" target="_blank" rel="noopener noreferrer" class="nav-link external">
  English
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/vuejs/docs-next-zh-cn" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>基础</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/installation.html" class="sidebar-link">安装</a></li><li><a href="/guide/introduction.html" class="sidebar-link">介绍</a></li><li><a href="/guide/instance.html" class="sidebar-link">应用实例</a></li><li><a href="/guide/template-syntax.html" class="sidebar-link">模板语法</a></li><li><a href="/guide/computed.html" class="sidebar-link">计算属性和侦听器</a></li><li><a href="/guide/class-and-style.html" class="sidebar-link">Class 与 Style 绑定</a></li><li><a href="/guide/conditional.html" class="sidebar-link">条件渲染</a></li><li><a href="/guide/list.html" class="sidebar-link">列表渲染</a></li><li><a href="/guide/events.html" class="sidebar-link">事件处理</a></li><li><a href="/guide/forms.html" class="sidebar-link">表单输入绑定</a></li><li><a href="/guide/component-basics.html" class="sidebar-link">组件基础</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>深入组件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/component-registration.html" class="sidebar-link">组件注册</a></li><li><a href="/guide/component-props.html" class="sidebar-link">Props</a></li><li><a href="/guide/component-attrs.html" class="sidebar-link">非 Prop 的 Attribute</a></li><li><a href="/guide/component-custom-events.html" class="sidebar-link">自定义事件</a></li><li><a href="/guide/component-slots.html" class="sidebar-link">插槽</a></li><li><a href="/guide/component-provide-inject.html" class="sidebar-link">提供 / 注入</a></li><li><a href="/guide/component-dynamic-async.html" class="sidebar-link">动态组件 &amp; 异步组件</a></li><li><a href="/guide/component-template-refs.html" class="sidebar-link">模板引用</a></li><li><a href="/guide/component-edge-cases.html" class="sidebar-link">处理边界情况</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>过渡&amp;动画</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/transitions-overview.html" class="sidebar-link">过渡 &amp; 动画概述</a></li><li><a href="/guide/transitions-enterleave.html" class="sidebar-link">进入过渡 &amp; 离开过渡</a></li><li><a href="/guide/transitions-list.html" class="sidebar-link">列表过渡</a></li><li><a href="/guide/transitions-state.html" class="sidebar-link">状态过渡</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>可复用性&amp;组合</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/mixins.html" class="sidebar-link">混入</a></li><li><a href="/guide/custom-directive.html" class="sidebar-link">自定义指令</a></li><li><a href="/guide/teleport.html" class="sidebar-link">传入</a></li><li><a href="/guide/render-function.html" class="sidebar-link">渲染函数</a></li><li><a href="/guide/plugins.html" class="sidebar-link">插件</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>高阶指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>响应性</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>组合 API</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/composition-api-introduction.html" aria-current="page" class="active sidebar-link">介绍</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/guide/composition-api-introduction.html#什么是-composition-api" class="sidebar-link">什么是 Composition API？</a></li><li class="sidebar-sub-header"><a href="/guide/composition-api-introduction.html#composition-api-基础" class="sidebar-link">Composition API 基础</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/guide/composition-api-introduction.html#setup-组件选项" class="sidebar-link">setup 组件选项</a></li><li class="sidebar-sub-header"><a href="/guide/composition-api-introduction.html#带-ref-的响应式变量" class="sidebar-link">带 ref 的响应式变量</a></li><li class="sidebar-sub-header"><a href="/guide/composition-api-introduction.html#生命周期钩子注册内部-setup" class="sidebar-link">生命周期钩子注册内部 setup</a></li><li class="sidebar-sub-header"><a href="/guide/composition-api-introduction.html#watch-响应式更改" class="sidebar-link">watch 响应式更改</a></li><li class="sidebar-sub-header"><a href="/guide/composition-api-introduction.html#独立的-computed-属性" class="sidebar-link">独立的 computed 属性</a></li></ul></li></ul></li><li><a href="/guide/composition-api-setup.html" class="sidebar-link">Setup</a></li><li><a href="/guide/composition-api-lifecycle-hooks.html" class="sidebar-link">生命周期钩子</a></li><li><a href="/guide/composition-api-provide-inject.html" class="sidebar-link">提供/注入</a></li><li><a href="/guide/composition-api-template-refs.html" class="sidebar-link">模板引用</a></li></ul></section></li><li><a href="/guide/optimizations.html" class="sidebar-link">渲染机制和优化</a></li><li><a href="/guide/change-detection.html" class="sidebar-link">Vue 2 中的更改检测警告</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>工具</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/single-file-component.html" class="sidebar-link">单文件组件</a></li><li><a href="/guide/testing.html" class="sidebar-link">测试</a></li><li><a href="/guide/typescript-support.html" class="sidebar-link">TypeScript 支持</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>规模化</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/routing.html" class="sidebar-link">路由</a></li><li><a href="/guide/state-management.html" class="sidebar-link">状态管理</a></li><li><a href="/guide/ssr.html" class="sidebar-link">服务端渲染</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>无障碍</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/guide/a11y-basics.html" class="sidebar-link">基础</a></li><li><a href="/guide/a11y-semantics.html" class="sidebar-link">语义学</a></li><li><a href="/guide/a11y-standards.html" class="sidebar-link">标准</a></li><li><a href="/guide/a11y-resources.html" class="sidebar-link">资源</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>从 Vue 2 迁移</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>贡献文档</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="介绍"><a href="#介绍" class="header-anchor">#</a> 介绍</h1> <h2 id="什么是-composition-api"><a href="#什么是-composition-api" class="header-anchor">#</a> 什么是 Composition API？</h2> <div class="custom-block tip"><p class="custom-block-title">提示</p> <p>在阅读文档之前，你应该已经熟悉了这两个 <a href="/guide/introduction.html">Vue 基础</a> 和<a href="/guide/component-basics.html">创建组件</a>。</p></div> <p>通过创建 Vue 组件，我们可以将接口的可重复部分及其功能提取到可重用的代码段中。仅此一项就可以使我们的应用程序在可维护性和灵活性方面走得更远。然而，我们的经验已经证明，光靠这一点可能是不够的，尤其是当你的应用程序变得非常大的时候——想想几百个组件。在处理如此大的应用程序时，共享和重用代码变得尤为重要。</p> <p>假设在我们的应用程序中，我们有一个视图来显示某个用户的仓库列表。除此之外，我们还希望应用搜索和筛选功能。处理此视图的组件可能如下所示：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  components<span class="token operator">:</span> <span class="token punctuation">{</span> RepositoriesFilters<span class="token punctuation">,</span> RepositoriesSortBy<span class="token punctuation">,</span> RepositoriesList <span class="token punctuation">}</span><span class="token punctuation">,</span>
  props<span class="token operator">:</span> <span class="token punctuation">{</span>
    user<span class="token operator">:</span> <span class="token punctuation">{</span> type<span class="token operator">:</span> String <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      repositories<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 1</span>
      filters<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
      searchQuery<span class="token operator">:</span> <span class="token string">''</span> <span class="token comment">// 2</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  computed<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">filteredRepositories</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
    <span class="token function">repositoriesMatchingSearchQuery</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 2</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  watch<span class="token operator">:</span> <span class="token punctuation">{</span>
    user<span class="token operator">:</span> <span class="token string">'getUserRepositories'</span> <span class="token comment">// 1</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  methods<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">getUserRepositories</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 使用 `this.user` 获取用户仓库</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 1</span>
    <span class="token function">updateFilters</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUserRepositories</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><p>该组件有以下几个职责：</p> <ol><li>从假定的外部 API 获取该用户名的仓库，并在用户更改时刷新它</li> <li>使用 <code>searchQuery</code> 字符串搜索存储库</li> <li>使用 <code>filters</code> 对象筛选仓库</li></ol> <p>用组件的选项 (<code>data</code>、<code>computed</code>、<code>methods</code>、<code>watch</code>) 组织逻辑在大多数情况下都有效。然而，当我们的组件变得更大时，<strong>逻辑关注点</strong>的列表也会增长。这可能会导致组件难以阅读和理解，尤其是对于那些一开始就没有编写这些组件的人来说。</p> <p><img src="https://user-images.githubusercontent.com/499550/62783021-7ce24400-ba89-11e9-9dd3-36f4f6b1fae2.png" alt="Vue 选项 API: 按选项类型分组的代码"></p> <p>一个大型组件的示例，其中<strong>逻辑关注点</strong>是按颜色分组。</p> <p>这种碎片化使得理解和维护复杂组件变得困难。选项的分离掩盖了潜在的逻辑问题。此外，在处理单个逻辑关注点时，我们必须不断地“跳转”相关代码的选项块。</p> <p>如果我们能够将与同一个逻辑关注点相关的代码配置在一起会更好。而这正是 Composition API 使我们能够做到的。</p> <h2 id="composition-api-基础"><a href="#composition-api-基础" class="header-anchor">#</a> Composition API 基础</h2> <p>既然我们知道了<strong>为什么</strong>，我们就可以知道<strong>怎么做</strong>。为了开始使用 Composition api，我们首先需要一个可以实际使用它的地方。在 Vue 组件中，我们将此位置称为 <code>setup</code>。</p> <h3 id="setup-组件选项"><a href="#setup-组件选项" class="header-anchor">#</a> <code>setup</code> 组件选项</h3> <p>新的 <code>setup</code> 组件选项在<strong>创建组件之前</strong>执行，一旦 <code>props</code> 被解析，并充当合成 API 的入口点。</p> <div class="custom-block warning"><p class="custom-block-title">WARNING</p> <p>由于在执行 <code>setup</code> 时尚未创建组件实例，因此在 <code>setup</code> 选项中没有 <code>this</code>。这意味着，除了 <code>props</code> 之外，你将无法访问组件中声明的任何属性——<strong>本地状态</strong>、<strong>计算属性</strong>或<strong>方法</strong>。</p></div> <p><code>setup</code> 选项应该是一个接受 <code>props</code> 和 <code>context</code> 的函数，我们将在<a href="/guide/composition-api-setup.html#参数">稍后</a>讨论。此外，我们从 <code>setup</code> 返回的所有内容都将暴露给组件的其余部分 (计算属性、方法、生命周期钩子等等) 以及组件的模板。</p> <p>让我们添加 <code>setup</code> 到我们的组件中：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  components<span class="token operator">:</span> <span class="token punctuation">{</span> RepositoriesFilters<span class="token punctuation">,</span> RepositoriesSortBy<span class="token punctuation">,</span> RepositoriesList <span class="token punctuation">}</span><span class="token punctuation">,</span>
  props<span class="token operator">:</span> <span class="token punctuation">{</span>
    user<span class="token operator">:</span> <span class="token punctuation">{</span> type<span class="token operator">:</span> String <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">setup</span><span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span> <span class="token comment">// { user: '' }</span>

    <span class="token keyword">return</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// 这里返回的任何内容都可以用于组件的其余部分</span>
  <span class="token punctuation">}</span>
  <span class="token comment">// 组件的“其余部分”</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>现在让我们从提取第一个逻辑关注点开始 (在原始代码段中标记为“1”)。</p> <blockquote><ol><li>从假定的外部 API 获取该用户名的仓库，并在用户更改时刷新它</li></ol></blockquote> <p>我们将从最明显的部分开始：</p> <ul><li>仓库列表</li> <li>更新仓库列表的函数</li> <li>返回列表和函数，以便其他组件选项可以访问它们</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue `setup` function</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchUserRepositories <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/api/repositories'</span>

<span class="token comment">// 在我们的组件内</span>
<span class="token function">setup</span> <span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> repositories <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">const</span> <span class="token function-variable function">getUserRepositories</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    repositories <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetchUserRepositories</span><span class="token punctuation">(</span>props<span class="token punctuation">.</span>user<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">,</span>
    getUserRepositories <span class="token comment">// 返回的函数与方法的行为相同</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>这是我们的出发点，但它还不能工作，因为我们的 <code>repositories</code> 变量不是被动的。这意味着从用户的角度来看，仓库列表将保持为空。我们来解决这个问题！</p> <h3 id="带-ref-的响应式变量"><a href="#带-ref-的响应式变量" class="header-anchor">#</a> 带 <code>ref</code> 的响应式变量</h3> <p>在 Vue 3.0 中，我们可以通过一个新的 <code>ref</code> 函数使任何响应式变量在任何地方起作用，如下所示：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ref <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> counter <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>ref</code> 接受参数并返回它包装在具有 <code>value</code> property 的对象中，然后可以使用该 property 访问或更改响应式变量的值：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ref <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> counter <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>counter<span class="token punctuation">)</span> <span class="token comment">// { value: 0 }</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>counter<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token comment">// 0</span>

counter<span class="token punctuation">.</span>value<span class="token operator">++</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>counter<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token comment">// 1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>在对象中包装值似乎不必要，但在 JavaScript 中保持不同数据类型的行为统一是必需的。这是因为在 JavaScript 中，<code>Number</code> 或 <code>String</code> 等基本类型是通过值传递的，而不是通过引用传递的：</p> <p><img src="https://blog.penjee.com/wp-content/uploads/2015/02/pass-by-reference-vs-pass-by-value-animation.gif" alt="按引用传递与按值传递"></p> <p>在任何值周围都有一个包装器对象，这样我们就可以在整个应用程序中安全地传递它，而不必担心在某个地方失去它的响应式。</p> <div class="custom-block tip"><p class="custom-block-title">提示</p> <p>换句话说，<code>ref</code> 对我们的值创建了一个<strong>响应式引用</strong>。使用<strong>引用</strong>的概念将在整个 Composition API 中经常使用。</p></div> <p>回到我们的例子，让我们创建一个响应式的 <code>repositories</code> 变量：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue `setup` function</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchUserRepositories <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/api/repositories'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> ref <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token comment">// in our component</span>
<span class="token function">setup</span> <span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> repositories <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> <span class="token function-variable function">getUserRepositories</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetchUserRepositories</span><span class="token punctuation">(</span>props<span class="token punctuation">.</span>user<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">,</span>
    getUserRepositories
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>完成！现在，每当我们调用 <code>getUserRepositories</code> 时，<code>repositories</code> 都将发生变化，视图将更新以反映更改。我们的组件现在应该如下所示：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchUserRepositories <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/api/repositories'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> ref <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  components<span class="token operator">:</span> <span class="token punctuation">{</span> RepositoriesFilters<span class="token punctuation">,</span> RepositoriesSortBy<span class="token punctuation">,</span> RepositoriesList <span class="token punctuation">}</span><span class="token punctuation">,</span>
  props<span class="token operator">:</span> <span class="token punctuation">{</span>
    user<span class="token operator">:</span> <span class="token punctuation">{</span> type<span class="token operator">:</span> String <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">setup</span> <span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> repositories <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">const</span> <span class="token function-variable function">getUserRepositories</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      repositories<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetchUserRepositories</span><span class="token punctuation">(</span>props<span class="token punctuation">.</span>user<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      repositories<span class="token punctuation">,</span>
      getUserRepositories
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      filters<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
      searchQuery<span class="token operator">:</span> <span class="token string">''</span> <span class="token comment">// 2</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  computed<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">filteredRepositories</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
    <span class="token function">repositoriesMatchingSearchQuery</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 2</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  watch<span class="token operator">:</span> <span class="token punctuation">{</span>
    user<span class="token operator">:</span> <span class="token string">'getUserRepositories'</span> <span class="token comment">// 1</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  methods<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">updateFilters</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUserRepositories</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><p>我们已经将第一个逻辑关注点中的几个部分移到了 <code>setup</code> 方法中，它们彼此非常接近。剩下的就是在 <code>mounted</code> 钩子中调用 <code>getUserRepositories</code>，并设置一个监听器，以便在 <code>user</code> prop 发生变化时执行此操作。</p> <p>我们将从生命周期钩子开始。</p> <h3 id="生命周期钩子注册内部-setup"><a href="#生命周期钩子注册内部-setup" class="header-anchor">#</a> 生命周期钩子注册内部 <code>setup</code></h3> <p>为了使 Composition API 的特性与选项 API 相比更加完整，我们还需要一种在 <code>setup</code> 中注册生命周期钩子的方法。这要归功于从 Vue 导出的几个新函数。Composition API 上的生命周期钩子与选项 API 的名称相同，但前缀为 <code>on</code>：即 <code>mounted</code> 看起来像 <code>onMounted</code>。</p> <p>这些函数接受在组件调用钩子时将执行的回调。</p> <p>让我们将其添加到 <code>setup</code> 函数中：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue `setup` function</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchUserRepositories <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/api/repositories'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token comment">// in our component</span>
<span class="token function">setup</span> <span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> repositories <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> <span class="token function-variable function">getUserRepositories</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetchUserRepositories</span><span class="token punctuation">(</span>props<span class="token punctuation">.</span>user<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token function">onMounted</span><span class="token punctuation">(</span>getUserRepositories<span class="token punctuation">)</span> <span class="token comment">// on `mounted` call `getUserRepositories`</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">,</span>
    getUserRepositories
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>现在我们需要对 <code>user</code> prop 所做的更改做出反应。为此，我们将使用独立的 <code>watch</code> 函数。</p> <h3 id="watch-响应式更改"><a href="#watch-响应式更改" class="header-anchor">#</a> <code>watch</code> 响应式更改</h3> <p>就像我们如何使用 <code>watch</code> 选项在组件内的 <code>user</code> property 上设置侦听器一样，我们也可以使用从 Vue 导入的 <code>watch</code> 函数执行相同的操作。它接受 3 个参数：</p> <ul><li>一个<strong>响应式引用</strong>或我们想要侦听的 getter 函数</li> <li>一个回调</li> <li>可选的配置选项</li></ul> <p><strong>下面让我们快速了解一下它是如何工作的</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> watch <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> counter <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
<span class="token function">watch</span><span class="token punctuation">(</span>counter<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">newValue<span class="token punctuation">,</span> oldValue</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'The new counter value is: '</span> <span class="token operator">+</span> counter<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>例如，每当 <code>counter</code> 被修改时 <code>counter.value=5</code>，watch 将触发并执行回调 (第二个参数)，在本例中，它将把 <code>'The new counter value is:5'</code> 记录到我们的控制台中。</p> <p><strong>以下是等效的选项 API ：</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      counter<span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  watch<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">counter</span><span class="token punctuation">(</span><span class="token parameter">newValue<span class="token punctuation">,</span> oldValue</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'The new counter value is: '</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>counter<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>有关 <code>watch</code> 的详细信息，请参阅我们的<a href="/guide/composition-api-introduction.html" aria-current="page" class="router-link-exact-active router-link-active">深入指南</a>。</p> <p><strong>现在我们将其应用到我们的示例中：</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue `setup` function</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchUserRepositories <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/api/repositories'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted<span class="token punctuation">,</span> watch<span class="token punctuation">,</span> toRefs <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token comment">// 在我们组件中</span>
<span class="token function">setup</span> <span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 使用 `toRefs` 创建对prop的 `user` property 的响应式引用</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> user <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">toRefs</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span>

  <span class="token keyword">const</span> repositories <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> <span class="token function-variable function">getUserRepositories</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 更新`prop.user ` 到 `user.value`访问引用值</span>
    repositories<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetchUserRepositories</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token function">onMounted</span><span class="token punctuation">(</span>getUserRepositories<span class="token punctuation">)</span>

  <span class="token comment">// 在用户prop的响应式引用上设置一个侦听器</span>
  <span class="token function">watch</span><span class="token punctuation">(</span>user<span class="token punctuation">,</span> getUserRepositories<span class="token punctuation">)</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">,</span>
    getUserRepositories
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>你可能已经注意到在我们的 <code>setup</code> 的顶部使用了 <code>toRefs</code>。这是为了确保我们的侦听器能够对 <code>user</code> prop 所做的更改做出反应。</p> <p>有了这些变化，我们就把第一个逻辑关注点移到了一个地方。我们现在可以对第二个关注点执行相同的操作——基于 <code>searchQuery</code> 进行过滤，这次是使用计算属性。</p> <h3 id="独立的-computed-属性"><a href="#独立的-computed-属性" class="header-anchor">#</a> 独立的 <code>computed</code> 属性</h3> <p>与 <code>ref</code> 和 <code>watch</code> 类似，也可以使用从 Vue 导入的 <code>computed</code> 函数在 Vue 组件外部创建计算属性。让我们回到我们的 counter 例子：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> computed <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">const</span> counter <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> twiceTheCounter <span class="token operator">=</span> <span class="token function">computed</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> counter<span class="token punctuation">.</span>value <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">)</span>

counter<span class="token punctuation">.</span>value<span class="token operator">++</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>counter<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token comment">// 1</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>twiceTheCounter<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token comment">// 2</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>在这里，<code>computed</code> 函数返回一个作为 <code>computed</code> 的第一个参数传递的 getter 类回调的输出的一个<em>只读</em>的<strong>响应式引用</strong>。为了访问新创建的计算变量的 <strong>value</strong>，我们需要像使用 <code>ref</code> 一样使用 <code>.value</code> property。</p> <p>让我们将搜索功能移到 <code>setup</code> 中：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue `setup` function</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchUserRepositories <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/api/repositories'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted<span class="token punctuation">,</span> watch<span class="token punctuation">,</span> toRefs<span class="token punctuation">,</span> computed <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token comment">// in our component</span>
<span class="token function">setup</span> <span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 使用 `toRefs` 创建对props的 `user` property 的响应式引用</span>
  <span class="token keyword">const</span> <span class="token punctuation">{</span> user <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">toRefs</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span>

  <span class="token keyword">const</span> repositories <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> <span class="token function-variable function">getUserRepositories</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 更新`props.user ` 到 `user.value`访问引用值</span>
    repositories<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetchUserRepositories</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token function">onMounted</span><span class="token punctuation">(</span>getUserRepositories<span class="token punctuation">)</span>

  <span class="token comment">// 在用户prop的响应式引用上设置一个侦听器</span>
  <span class="token function">watch</span><span class="token punctuation">(</span>user<span class="token punctuation">,</span> getUserRepositories<span class="token punctuation">)</span>

  <span class="token keyword">const</span> searchQuery <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> repositoriesMatchingSearchQuery <span class="token operator">=</span> <span class="token function">computed</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> repositories<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>
      <span class="token parameter">repository</span> <span class="token operator">=&gt;</span> repository<span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>searchQuery<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">,</span>
    getUserRepositories<span class="token punctuation">,</span>
    searchQuery<span class="token punctuation">,</span>
    repositoriesMatchingSearchQuery
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p>对于其他的<strong>逻辑关注点</strong>我们也可以这样做，但是你可能已经在问这个问题了——<em>这不就是把代码移到 <code>setup</code> 选项并使它变得非常大吗</em>？嗯，那是真的。这就是为什么在继续其他任务之前，我们将首先将上述代码提取到一个独立的<strong>组合函数</strong>。让我们从创建 <code>useUserRepositories</code> 开始：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/composables/useUserRepositories.js</span>

<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchUserRepositories <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/api/repositories'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted<span class="token punctuation">,</span> watch<span class="token punctuation">,</span> toRefs <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">useUserRepositories</span><span class="token punctuation">(</span><span class="token parameter">user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> repositories <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> <span class="token function-variable function">getUserRepositories</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetchUserRepositories</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token function">onMounted</span><span class="token punctuation">(</span>getUserRepositories<span class="token punctuation">)</span>
  <span class="token function">watch</span><span class="token punctuation">(</span>user<span class="token punctuation">,</span> getUserRepositories<span class="token punctuation">)</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    repositories<span class="token punctuation">,</span>
    getUserRepositories
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>然后是搜索功能：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/composables/useRepositoryNameSearch.js</span>

<span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted<span class="token punctuation">,</span> watch<span class="token punctuation">,</span> toRefs <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">useRepositoryNameSearch</span><span class="token punctuation">(</span><span class="token parameter">repositories</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> searchQuery <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> repositoriesMatchingSearchQuery <span class="token operator">=</span> <span class="token function">computed</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> repositories<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token parameter">repository</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> repository<span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>searchQuery<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    searchQuery<span class="token punctuation">,</span>
    repositoriesMatchingSearchQuery
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p><strong>现在在单独的文件中有了这两个功能，我们就可以开始在组件中使用它们了。以下是如何做到这一点：</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue</span>
<span class="token keyword">import</span> useUserRepositories <span class="token keyword">from</span> <span class="token string">'@/composables/useUserRepositories'</span>
<span class="token keyword">import</span> useRepositoryNameSearch <span class="token keyword">from</span> <span class="token string">'@/composables/useRepositoryNameSearch'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> toRefs <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  components<span class="token operator">:</span> <span class="token punctuation">{</span> RepositoriesFilters<span class="token punctuation">,</span> RepositoriesSortBy<span class="token punctuation">,</span> RepositoriesList <span class="token punctuation">}</span><span class="token punctuation">,</span>
  props<span class="token operator">:</span> <span class="token punctuation">{</span>
    user<span class="token operator">:</span> <span class="token punctuation">{</span> type<span class="token operator">:</span> String <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">setup</span> <span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> user <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">toRefs</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span>

    <span class="token keyword">const</span> <span class="token punctuation">{</span> repositories<span class="token punctuation">,</span> getUserRepositories <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">useUserRepositories</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>

    <span class="token keyword">const</span> <span class="token punctuation">{</span>
      searchQuery<span class="token punctuation">,</span>
      repositoriesMatchingSearchQuery
    <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">useRepositoryNameSearch</span><span class="token punctuation">(</span>repositories<span class="token punctuation">)</span>

    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      <span class="token comment">// 因为我们并不关心未经过滤的仓库</span>
      <span class="token comment">// 我们可以在 `repositories` 名称下暴露过滤后的结果</span>
      repositories<span class="token operator">:</span> repositoriesMatchingSearchQuery<span class="token punctuation">,</span>
      getUserRepositories<span class="token punctuation">,</span>
      searchQuery<span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      filters<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  computed<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">filteredRepositories</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  methods<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">updateFilters</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 3</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><p>此时，你可能已经知道了这个练习，所以让我们跳到最后，迁移剩余的过滤功能。我们不需要深入了解实现细节，因为这不是本指南的重点。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// src/components/UserRepositories.vue</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> toRefs <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">import</span> useUserRepositories <span class="token keyword">from</span> <span class="token string">'@/composables/useUserRepositories'</span>
<span class="token keyword">import</span> useRepositoryNameSearch <span class="token keyword">from</span> <span class="token string">'@/composables/useRepositoryNameSearch'</span>
<span class="token keyword">import</span> useRepositoryFilters <span class="token keyword">from</span> <span class="token string">'@/composables/useRepositoryFilters'</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  components<span class="token operator">:</span> <span class="token punctuation">{</span> RepositoriesFilters<span class="token punctuation">,</span> RepositoriesSortBy<span class="token punctuation">,</span> RepositoriesList <span class="token punctuation">}</span><span class="token punctuation">,</span>
  props<span class="token operator">:</span> <span class="token punctuation">{</span>
    user<span class="token operator">:</span> <span class="token punctuation">{</span> type<span class="token operator">:</span> String <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">setup</span><span class="token punctuation">(</span><span class="token parameter">props</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> user <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">toRefs</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span>

    <span class="token keyword">const</span> <span class="token punctuation">{</span> repositories<span class="token punctuation">,</span> getUserRepositories <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">useUserRepositories</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>

    <span class="token keyword">const</span> <span class="token punctuation">{</span>
      searchQuery<span class="token punctuation">,</span>
      repositoriesMatchingSearchQuery
    <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">useRepositoryNameSearch</span><span class="token punctuation">(</span>repositories<span class="token punctuation">)</span>

    <span class="token keyword">const</span> <span class="token punctuation">{</span>
      filters<span class="token punctuation">,</span>
      updateFilters<span class="token punctuation">,</span>
      filteredRepositories
    <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">useRepositoryFilters</span><span class="token punctuation">(</span>repositoriesMatchingSearchQuery<span class="token punctuation">)</span>

    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      <span class="token comment">// 因为我们并不关心未经过滤的仓库</span>
      <span class="token comment">// 我们可以在 `repositories` 名称下暴露过滤后的结果</span>
      repositories<span class="token operator">:</span> filteredRepositories<span class="token punctuation">,</span>
      getUserRepositories<span class="token punctuation">,</span>
      searchQuery<span class="token punctuation">,</span>
      filters<span class="token punctuation">,</span>
      updateFilters
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><p>我们完成了！</p> <p>请记住，我们只触及了 Composition API 的表面以及它允许我们做什么。要了解更多信息，请参阅深入指南。</p></div> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/guide/reactivity-computed-watchers.html" class="prev">响应式计算和侦听</a></span> <span class="next"><a href="/guide/composition-api-setup.html">Setup</a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.0a9bf1a0.js" defer></script><script src="/assets/js/9.f307b0a5.js" defer></script><script src="/assets/js/18.9c9769d1.js" defer></script><script src="/assets/js/10.523605bb.js" defer></script><script src="/assets/js/85.02c708f2.js" defer></script>
  </body>
</html>
